﻿
Imports std
Imports xmlctrl
Imports xhtmlctrl
Imports webctrl
Imports storoomc

Public Class listview
    Inherits cacheControlProcessor

    Public Const _pathidentity As String = network.pathSeperator + "list"

    Public Overrides Function pathidentity() As String
        Return _pathidentity
    End Function

    Protected Overrides Function normalRetiredTicks() As Long
        Return secondsToTicks(10)
    End Function

    Public Shared Function generatelink(ByVal host As String, ByVal port As String) As String
        Dim rtn As String = Nothing
        rtn = strmid(_pathidentity, strlen(network.pathSeperator))
        rtn = urlUtils.appendQuery(rtn, query.host, host, query.port, port)
        Return rtn
    End Function

    Public Shared Function generatelink(ByVal host As String, ByVal port As Int32) As String
        Return generatelink(host, Convert.ToString(port))
    End Function

    Protected Overrides Function processImpl(ByVal context As httpContext, ByRef control As xmlcontrol) As Boolean
        assert(Not context Is Nothing, "context is nothing.")
        Dim page As rvpage = Nothing
        page = context.createControl(New rvpage())
        page.addcsslink(cssrequest.generateCssfileLinkUrl("listview.css"))
        page.title().text = context.string(stringId.LISTVIEW_TITLE)

        Dim host As String = Nothing
        Dim port As Int32 = Nothing
        host = context.url().query(query.host)
        port = context.url().queryInt32(query.port)
        If isemptystring(host) OrElse port = npos Then
            Dim div As div = Nothing
            div = context.createControl(New div())
            div.text = context.string(stringId.NO_HOST_OR_PORT)
            page.leftrail().addchild(div)
        Else
            Dim talk As storoomTalk = Nothing
            talk = roomview.talk(host, port)
            If talk.health() Then
                Dim s As [set](Of String) = Nothing
                If Not talk.list(s) Then
                    Dim div As div = Nothing
                    div = context.createControl(New div())
                    div.text = context.string(stringId.TALK_FAILED)
                    page.leftrail().addchild(div)
                Else
                    Dim ul As ul = Nothing
                    ul = context.createControl(New ul())
                    page.leftrail().addchild(ul)
                    Dim it As [set](Of String).iterator = Nothing
                    it = s.begin()
                    While it <> s.end()
                        Dim li As li = Nothing
                        li = context.createControl(New colorli(page))
                        ul.addchild(li)
                        Dim a As a = Nothing
                        a = context.createControl(New a())
                        a.text = (+it)
                        a.href = keyview.generatelink(host, port, (+it))
                        li.addchild(a)

                        it += 1
                    End While
                End If
            Else
                page.leftrail().text = context.string(stringId.TALK_FAILED)
            End If
        End If

        control = page
        Return True
    End Function
End Class
